<script type="text/javascript" src="../media/system/js/jQuery.vietvang.validate.js"></script>
    <script type="text/javascript">
        jq(document).ready(function(){
            jq('#adminForm').validate({
            });
        });
</script>
<form action="index.php?option=com_statistic&disp=center" method="post" id="adminForm" name="adminForm" class="adminform">
    
    <table id="form_table">
    	<tr>
    		<td class="text_right"> センター・クラブ</td>
    		<td style="width: 15%;"><input readonly id="ocba_code" name="ocba_code" type="text" class="inputbox medium" 
                <?php if(in_array('11', explode(',',$this->aut->getUserGroup()))) echo "disabled=\"disabled\""?> value="<?php echo $this->data['ocba_code']?>"></td>
            <td style="width: 5%;"><a><input id="a00871" type="button" value="参照"
                 <?php if(in_array('11', explode(',',$this->aut->getUserGroup()))) echo "disabled=\"disabled\""?> /> </a> </td>
            <td><input name="ocba_name" id="ocba_name" type="text" disabled="true" value="<?php echo $this->data['ocba_name']?>" class="big"></td>
    	</tr>
    	<tr>
    		<td class="text_right">年月</td>
    		<td colspan="3">
                <select id="year" name="year" class="inputbox small">
               	<?php
                echo HelperBG::getYearOption(JRequest::getVar('year'), true);
                ?>
                </select>
                <select id="month" name="month" class="">
                <?php
                echo HelperBG::getMonthOption(JRequest::getVar('month'), true);
                ?>
                </select>
            </td>

    	</tr>
    </table>
    <table  class="adminlist tb_input" style="display: <?php echo empty($this->data) ? "none" : "table"?>;">
        <thead>
			<tr>
            <th width="20%" class="left">
								
            </th>
            <?php
            if(count($this->usfm) != 0){
            $width = 80/count($this->usfm);
            foreach($this->usfm as $fm_row){
                echo "  <th width='$width%' class='left'>
        			         $fm_row->USFM_NAME				
                        </th>";
            }
            ?>
				
			</tr>
		</thead>
		
		<tbody>
        <?php
        foreach($this->uscm as $k=>$cm_row){
            //$row_cls = $k%2;
            echo "<tr class='row0'>			
        			<td class='left'>$cm_row->USCM_NAME</td>";
            foreach($this->usfm as $fm_row){
                $value = $this->data['usce_data'][$cm_row->USCM_CODE][$fm_row->USFM_CODE];
                echo "<td class='left'><input vvname='digits' name='usce_data[$cm_row->USCM_CODE][$fm_row->USFM_CODE]' type='text' class='fm$fm_row->USFM_CODE sumable$cm_row->USCM_NUMF' style='width:100%;text-align:right' 
                value='$value'> <span name='msg_error'></span></td>";
            }
            echo  "</tr>";
        }
        ?>
            <tr class="row0">			
    			<td class="left">人数合計</td>
                <?php
                foreach($this->usfm as $fm_row){
                    echo "<td class='left'><input id='total$fm_row->USFM_CODE' type='text' disabled='true' style='width:100%'> </td>";
                }
  //end if usfm != 0
  }
                ?>
    		  </tr>
		  
		</tbody>
    </table>
    <input type="hidden" name="task" />
</form>

<script>
    jq(document).ready(function() {
        <?php
        $count = count($this->usfm);
        foreach($this->usfm as $fm_row){
            echo "            
                jq(\"input.fm$fm_row->USFM_CODE\").each(function(){
                    jq(this).change(function(){
                        var sum = 0;
                        jq(\"input.fm$fm_row->USFM_CODE\").each(function(){
                            if(!isNaN(parseInt(jq(this).val(),10)) && jq(this).hasClass('sumable1'))
                                sum = sum + parseInt(jq(this).val(),10);
                            //console.log(jq(this).val());
                        });
                        jq('#total$fm_row->USFM_CODE').val(sum);
                    });
                });
                
                jq(\"input.fm$fm_row->USFM_CODE\").each(function(){
                    var sum = 0;
                    jq(\"input.fm$fm_row->USFM_CODE\").each(function(){
                        if(!isNaN(parseInt(jq(this).val(),10)) && jq(this).hasClass('sumable1'))
                            sum = sum + parseInt(jq(this).val(),10);
                        //console.log(jq(this).val());
                    });
                    jq('#total$fm_row->USFM_CODE').val(sum);
                });
            ";
        }
        ?>

        // show a dialog box when clicking on a link
        jq('#a00871').bind('click', function(e) {
            e.preventDefault();
            dialog = new jq.Zebra_Dialog('', {
                source:  {'ajax': 'index.php?option=com_popupstatistic&views=bg&task=a00871&tmpl=component&type=center'},
                position:  ['center', 'top + 50'],
                width: 550,
                title:  'センター・クラブ検索',
                type:  '',
                buttons:  [
                    {caption: '閉じる', callback: function() { }}
                    
                ]
            });
        }); 
        
        //ocba_code change event
        jq('#ocba_code, #year, #month').change(function(){
            reload();
        });     
        
             
     });
     
     function select_ocba(code,name){
        jq('#ocba_code').val(code);
        jq('#ocba_name').val(name);
        dialog.close();
        reload();
     }
     
    function reload(){
        if(jq('#ocba_code').val()=='')
            return;
        var params = "&month="+jq('#month option:selected').val()
                        +"&year="+jq('#year option:selected').val()
                        +"&ocba="+jq('#ocba_code').val();
        window.location.href = "index.php?option=com_statistic&disp=center&task=form"+params;
     }
     
     
     
</script>

<script>
    Joomla.submitbutton = function(task){
	   if(task == 'back'){
            window.location.href = "index.php?option=com_statistic&disp=center&task=display";
	   }
       
       if(document.formvalidator.isValid(document.id('adminForm'))){
            Joomla.submitform(task, document.getElementById('adminForm'));
       }        
       
    }
</script>